package com.teaRestaurant.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teaRestaurant.domain.DTO.ComboPageQueryDTO;
import com.teaRestaurant.domain.entity.Combo;
import com.teaRestaurant.domain.entity.ComboDetail;
import com.teaRestaurant.domain.entity.Dishes;
import com.teaRestaurant.mapper.ComboDetailMapper;
import com.teaRestaurant.mapper.ComboMapper;
import com.teaRestaurant.service.ComboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ComboServiceImpl implements ComboService {

    @Autowired
    private ComboMapper comboMapper;
    @Autowired
    private ComboDetailMapper comboDetailMapper;
    @Override
    public PageInfo<Combo> list(ComboPageQueryDTO comboPageQueryDTO) {
        System.out.println(comboPageQueryDTO);
        int pageNum = comboPageQueryDTO.getPageNum();
        int pageSize = comboPageQueryDTO.getPageSize();
        List<Combo> comboList = comboMapper.list(comboPageQueryDTO);
        PageInfo<Combo> pageInfo = new PageInfo<>(comboList);
        // 手搓分页
        List<Combo> comboList2 = new ArrayList<>();
        for(int i = (pageNum - 1) * pageSize ;i<comboList.size() && i < pageSize * pageNum; i++){
            comboList2.add(comboList.get(i));
        }
        pageInfo.setList(comboList2);
        return pageInfo;
    }

    @Override
    public List<Combo> getById(Long id) {
        ComboPageQueryDTO combo = ComboPageQueryDTO.builder().id(id).build();
        combo.setStatus(1);
        List<Combo> comboList = comboMapper.list(combo);
        return comboList;
    }

    @Override
    public void insert(Combo combo) {
        combo.setOperateTime(LocalDateTime.now());
        comboMapper.insert(combo);
        Long comboId = combo.getId();
        // 插入套餐详情
        for (ComboDetail comboDetail : combo.getComboDetailList()) {
            comboDetail.setComboId(comboId);
            comboDetailMapper.insert(comboDetail);
        }
    }

    /**
     * TODO
     * @param combo
     */
    @Override
    @Transactional
    public void update(Combo combo) {
        System.out.println(combo);
        // 更新套餐信息
        combo.setOperateTime(LocalDateTime.now());
        LambdaQueryWrapper<Combo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Combo::getId,combo.getId());
        comboMapper.update(combo,wrapper);

        // 删除套餐详情信息
        LambdaQueryWrapper<ComboDetail> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(ComboDetail::getComboId,combo.getId());
        comboDetailMapper.delete(wrapper2);

        List<ComboDetail> comboDetailList = combo.getComboDetailList();
        for (ComboDetail comboDetail : comboDetailList) {
            comboDetail.setComboId(combo.getId());
            // 新增套餐详情信息
            comboDetailMapper.insert(comboDetail);
        }

    }

    @Override
    public void delete(List<Long> ids) {
        for(Long id: ids) {
            // 1、删除套餐详情
            comboMapper.deleteComboDetail(id);
            // 2、删除套餐
            comboMapper.deleteById(id);
        }
    }

    @Override
    public void updateStatus(Long id) {
        Combo combo = comboMapper.selectById(id);
        combo.setStatus(combo.getStatus() == 1?2:1);
        combo.setOperateTime(LocalDateTime.now());
        LambdaQueryWrapper<Combo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Combo::getId,combo.getId());
        comboMapper.update(combo,wrapper);
    }

    @Override
    public Map getComboSellCount() {
        LambdaQueryWrapper<Combo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Combo::getStatus,1);
        Long isSellCount = comboMapper.selectCount(wrapper);

        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Combo::getStatus,2);
        Long noSellCount = comboMapper.selectCount(wrapper);
        Map map = new HashMap<>();
        map.put("isSellCount",isSellCount);
        map.put("noSellCount",noSellCount);
        return map;
    }

}
